package com.useeinfo.oa.modules.base.biz;

import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.extend.biz.CrudBiz;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.SelectVO;
import com.useeinfo.framework.sugar.data.TableResultData;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.base.dao.PositionDao;
import com.useeinfo.oa.modules.base.entity.Enterprise;
import com.useeinfo.oa.modules.base.entity.Personnel;
import com.useeinfo.oa.modules.base.entity.Position;
import net.sf.json.JSONArray;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @program: YiYouFuPlatform
 * @author: Taogq
 * @createDate: 2018-12-04
 * @createTime: 16:12:33
 * service
 */
@Service
public class PositionBiz extends CrudBiz<PositionDao, Position> {


    public List<SelectVO> getPositionByOrgna(String organizationId, String enterpriseId) {
        return dao.getPositionByOrgna(organizationId,enterpriseId);
    }

    public List<Object> getPositionByArea(String areaId, String enterpriseId,int pageNow,int pageSize,int type,String authorityName) {
        return dao.getPositionByArea(areaId, enterpriseId,pageNow,pageSize,type,authorityName);
    }

    public ResultDto saveUpdate(Position position) {
        if (null == position.getId()) {
            Enterprise enterprise = position.getEnterprise();
            if (null == enterprise) {
                position.setCode(StringConverters.formatTime("yyyyMMdd", new Date()) + "-" + StringConverters.getRandomNum(3));
            } else {
                position.setCode(enterprise.getId() + "-" + StringConverters.formatTime("yyyyMMdd", new Date()) + "-" + StringConverters.getRandomNum(3));
            }
            position.setCreateDate(new Date());
            dao.add(position);
        } else {
            Position positionEdit = dao.findModel(position.getId());
            //positionEdit.setCode(position.getCode());
            positionEdit.setName(position.getName());
            dao.update(positionEdit);
        }
        return ResultUtil.success();
    }


    public List<Position> hqlExcute(String hql) {
        return dao.hqlExcute(hql);
    }

    public List<Position> hqlExcute(String hql, QueryParam queryParam) {
        return dao.hqlExcute(hql,queryParam);
    }

    /**
     * 根据区域获取岗位列表
     * @param areaId
     * @param enterpriseId
     * @return
     */
    public JSONObject getPositionJson(String areaId, String enterpriseId,String authorityName,String pageNow,String pageSize) {
        int no=StringConverters.null2Int(pageNow);
        int size=StringConverters.null2Int(pageSize);
        List<Object> objects = dao.getPositionByArea(areaId, enterpriseId,no,size,0,authorityName);
       com.alibaba.fastjson.JSONArray jsonArray=new com.alibaba.fastjson.JSONArray();
        if(objects!=null){
            objects.forEach(o -> {
                Position position=dao.findModel(StringConverters.null2Long(o));
                jsonArray.add(position.toJSONObject());
            });
        }
        int count=StringConverters.null2Int(dao.getPositionByArea(areaId, enterpriseId,no,size,1,authorityName).get(0));
        return TableResultData.Rsult(count, StringConverters.null2Int(pageNow), StringConverters.null2Int(pageSize), jsonArray);
    }



}
